﻿using Model.Data;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace WssClient.Item.XinDaXiang
{
    public class SalesStockOut : BaseItemData<SalesStockOut>
    {
        private string orderId = ""; //订单编号
        private string catNo = "";   //车号
        private string takeSite = "";   //收货地址
        private string orderDate = "";   //订单日期
        private string customer = "";    //客户
        private string tel = "";     //电话
        private decimal currentPaymentGoodsTotal = 0;  //本期货款合计
        private string upperCaseAmount = "";  //大写金额
        private decimal priorPeriodPaymentGoodsTotal = 0;  //上期货款合计
        private decimal qiChuQianKuanHeJi = 0;  //期初欠款合计
        private string single = "";  //制单人
        private string deliveryMan = ""; //送货人
        private List<Product> monkeyHeavyProducts = new List<Product>(); //跑马重产品列表
        private List<Product> generalProducts = new List<Product>(); //常规产品列表

        public string OrderId { get => orderId; set => orderId = value; }
        public string CatNo { get => catNo; set => catNo = value; }
        public string TakeSite { get => takeSite; set => takeSite = value; }
        public string OrderDate { get => orderDate; set => orderDate = value; }
        public string Customer { get => customer; set => customer = value; }
        public string Tel { get => tel; set => tel = value; }
        public decimal CurrentPaymentGoodsTotal { get => currentPaymentGoodsTotal; set => currentPaymentGoodsTotal = value; }
        public string UpperCaseAmount { get => upperCaseAmount; set => upperCaseAmount = value; }
        public decimal PriorPeriodPaymentGoodsTotal { get => priorPeriodPaymentGoodsTotal; set => priorPeriodPaymentGoodsTotal = value; }
        public decimal QiChuQianKuanHeJi { get => qiChuQianKuanHeJi; set => qiChuQianKuanHeJi = value; }
        public string Single { get => single; set => single = value; }
        public string DeliveryMan { get => deliveryMan; set => deliveryMan = value; }
        public List<Product> MonkeyHeavyProducts { get => monkeyHeavyProducts; set => monkeyHeavyProducts = value; }
        public List<Product> GeneralProducts { get => generalProducts; set => generalProducts = value; }

        public override void Analysis(RequestData requestData)
        {
            try
            {
                dynamic data = requestData.data;

                OrderId = CommonHelper.GetString(data, "orderId");
                CatNo = CommonHelper.GetString(data, "catNo");
                
                TakeSite = CommonHelper.GetString(data, "takeSite");
                if (TakeSite.Equals("{}"))
                {
                    TakeSite = "";
                }
                else
                {
                    TakeSite = TakeSite.Replace("\r\n", "");
                    TakeSite = TakeSite.Replace(" ", "");
                    dynamic dym = JsonHelper.DeserializeJsonToObject<dynamic>(TakeSite);

                    string province = CommonHelper.GetString(dym, "province");
                    string city = CommonHelper.GetString(dym, "city");
                    string district = CommonHelper.GetString(dym, "district");
                    string detail = CommonHelper.GetString(dym, "detail");

                    TakeSite = $"{province}{city}{district}{detail}";
                }


                OrderDate = CommonHelper.GetString(data, "orderDate");
                int odIdx = OrderDate.IndexOf(" ");
                if (odIdx > 0)
                {
                    OrderDate = OrderDate.Substring(0, odIdx);
                }

                Customer = CommonHelper.GetString(data, "customer");
                Tel = CommonHelper.GetString(data, "tel");
                
                CurrentPaymentGoodsTotal = CommonHelper.GetDecimal(data, "currentPaymentGoodsTotal");
                UpperCaseAmount = CommonHelper.GetString(data, "upperCaseAmount");
                PriorPeriodPaymentGoodsTotal = CommonHelper.GetDecimal(data, "priorPeriodPaymentGoodsTotal");
                QiChuQianKuanHeJi = CommonHelper.GetDecimal(data, "qiChuQianKuanHeJi");

                //dynamic operatorObj = CommonHelper.GetValue(data, "operator");
                //single = CommonHelper.GetString(operatorObj, "name");
                dynamic creatorObj = CommonHelper.GetValue(data, "creator");
                Single = CommonHelper.GetString(creatorObj, "name");

                string driverl = CommonHelper.GetString(data, "driver1");
                string driver2 = CommonHelper.GetString(data, "driver");
                string carFollowing = CommonHelper.GetString(data, "carFollowing");

                DeliveryMan = driverl;
                if (string.IsNullOrEmpty(DeliveryMan))
                {
                    DeliveryMan = driver2;
                }
                else
                {
                    DeliveryMan = string.IsNullOrEmpty(driver2) ? DeliveryMan : $"{DeliveryMan} {driver2}";
                }
                if (string.IsNullOrEmpty(DeliveryMan))
                {
                    DeliveryMan = carFollowing;
                }
                else
                {
                    DeliveryMan = string.IsNullOrEmpty(carFollowing) ? DeliveryMan : $"{DeliveryMan} {carFollowing}";
                }

                JArray monkeyHeavys = data.monkeyHeavy; //获取跑马重列表
                JArray generals = data.general;   //获取常规列表

                if (generals != null && generals.Count > 0)
                {
                    int idx = 1;
                    foreach (var item in generals)
                    {
                        Product generalProduct = Product.AnalysisGeneral(item);
                        generalProduct.Idx = idx++;
                        GeneralProducts.Add(generalProduct);
                    }
                }

                if (monkeyHeavys != null && monkeyHeavys.Count > 0)
                {
                    int idx = GeneralProducts.Count + 1;
                    foreach (var item in monkeyHeavys)
                    {
                        Product monkeyHeavyProduct = Product.AnalysisMonkeyHeavy(item);
                        monkeyHeavyProduct.Idx = idx++;
                        MonkeyHeavyProducts.Add(monkeyHeavyProduct);
                    }
                }
            }
            catch (Exception)
            {

            }
        }
    }
}
